package com.pku.jtestx86.popup.popup.actions;



import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.*;
import org.eclipse.jdt.internal.core.CompilationUnit;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;

import com.pku.jtestx86.views.ASTView;
import com.pku.ss.ui.ASTExplorer;

public class ASTAction implements IObjectActionDelegate {

	private Shell shell;
	private IWorkbenchPart targetPart;
	/**
	 * Constructor for Action1.
	 */
	public ASTAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
		this.targetPart=targetPart;
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		try {
			TreeSelection selection=(TreeSelection) this.targetPart.getSite().getSelectionProvider().getSelection();
			int segmentCount=selection.getPaths()[0].getSegmentCount();
			@SuppressWarnings("restriction")
			org.eclipse.jdt.internal.core.CompilationUnit compilationUnit=(org.eclipse.jdt.internal.core.CompilationUnit) selection.getPaths()[0].getSegment(segmentCount-1);
			ASTView viewPart = (ASTView) this.targetPart.getSite().getWorkbenchWindow().getActivePage().showView("com.pku.jtestx86.views.ASTView");
			//System.out.println(viewPart.getClass());
			viewPart.show(compilationUnit);
			
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		System.out.println(selection.isEmpty());
	}

}
